package client

import (
	"io"
	"net/http"
	"os"
	"strings"
)

func (client *client) DownloadFile(_url string, localpath string) error {
	// Get the data
	resp, err := http.Get(_url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	path := "."
	if strings.Contains(localpath, "/") {
		path = localpath[0:strings.LastIndex(localpath, "/")]
	}

	e := os.MkdirAll(path, 0x777)
	if e != nil {
		return err
	}

	// Create the file
	out, err := os.Create(localpath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	return err
}
